//
//  HMListViewModel.swift
//  Weibo
//
//  Created by ZhuDong on 16/5/25.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD

class HMListViewModel: NSObject {
    
    lazy var statusesViewModels: [HMStatusViewModel] = [HMStatusViewModel]()
    
    func loadData(isPullingUp: Bool,finish: (isSuccess: Bool,count:Int)->()){
        var max_id: Int64 = 0
        var since_id: Int64 = 0
        if isPullingUp {
            max_id = self.statusesViewModels.last?.status?.id ?? 0
        }else{
            since_id = self.statusesViewModels.first?.status?.id ?? 0
        }
        NetworkTools.tools.loadHomePageData(since_id,max_id: max_id) { (responseObject, error) -> (Void) in
            if error != nil{
                SVProgressHUD.showErrorWithStatus(errorStrOfNet)
                return
            }
            guard let dict: [String:AnyObject] = responseObject as? [String:AnyObject] else {
                return
            }
            let array = dict["statuses"] as! [[String:NSObject]]
            
            var arrayStatusViewModel = [HMStatusViewModel]()
            for item in array{
                let status = HMStatus(dict: item)
                let statuseViewModel = HMStatusViewModel()
                statuseViewModel.status = status
                arrayStatusViewModel.append(statuseViewModel)
            }
            if(isPullingUp){
                self.statusesViewModels = self.statusesViewModels +  arrayStatusViewModel
//                print("底部刷新数据\(arrayStatusViewModel)")
            }else{
                self.statusesViewModels = arrayStatusViewModel + self.statusesViewModels
            }
            finish(isSuccess: true, count: arrayStatusViewModel.count)
        }

    }
    
}
